args Controls CreativityMeasure


preserve

* setup
replace Creativity=creativityt_`CreativityMeasure'_7


local ipw=1
local SE      SE_I23     SE_I33     SE_I42     SE_I46     SE_I50     SE_I55


* calculate ipw weights
foreach y of local SE {
	if `ipw'==1{
		ge io=`y'!=.
		qui logit io $HDC_0
		drop io
		predict p_`y', pr
	}

	if `ipw'==0{
		ge  p_`y'=1
	}

}	


* reshape to long dataset
reshape long SE_I AE_I p_SE_I pse_SE_I, i(NCDSID) j(age)

* effect on self employment
rifhdreg SE  `Controls'  [pweight=1/p_SE], rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_SE

* effect on self employment conditional on being in work
rifhdreg SE  `Controls'  [pweight=1/p_SE] if AE==1, rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_SEgE

* effect on self employment w/ educ controls
rifhdreg SE  `Controls' mQual16 mQual18 mUniQual MissingQual16 MissingQual18 MissingUniQual [pweight=1/p_SE], rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_SEeduc

* effect on self employment conditional on being in work w/ educ controls
rifhdreg SE  `Controls' mQual16 mQual18 mUniQual MissingQual16 MissingQual18 MissingUniQual [pweight=1/p_SE] if AE==1, rif(mean) cluster(NCDSID)
mat b = e(b)
mat b = b'
svmat b
qui su b1 if b1!=0
mat define p= r(N)
drop b1
qui estadd scalar Par=p[1,1]
qui estadd scalar Ni=e(N_clust) 
estadd scalar mdv=e(rifmean) 
est sto es_SEgEeduc



* count number of variables
local count0: word count `O'
local count=`count0'+1


* self employment and employment in creative occupations
******************************************************************************************




local text1  ///
prehead(\\ \hline \hline ///
& \\ ///
\multicolumn{5}{l}{\textbf{Panel A: Without controls for educational attainment}}  \\ & \\ ///
&  \multicolumn{2}{c}{In self-employment} & \multicolumn{2}{c}{ \ In a creative occupation} \\ \cmidrule(r{10pt}){2-3} \cmidrule(l{10pt}){4-5}  ) ///
prefoot(\cmidrule(r{10pt}){2-3} \cmidrule(l{10pt}){4-5}   \vspace{-.3cm}\\ )  ///
posthead(\vspace{-.3cm}\\ \cmidrule(r{10pt}){2-3} \cmidrule(l{10pt}){4-5}  ) ///
postfoot(\hline \\ ) 

local text2  ///
prehead(& \\ /// 
 \multicolumn{5}{l}{\textbf{Panel B: With controls for educational attainment}}  \\ & \\  /// 
& \multicolumn{2}{c}{In self-employment} & \multicolumn{2}{c}{ \ In a creative occupation} \\ \cmidrule(r{10pt}){2-3} \cmidrule(l{10pt}){4-5}  ) ///
prefoot(\cmidrule(r{10pt}){2-3} \cmidrule(l{10pt}){4-5}   \vspace{-.3cm}\\ )  ///
posthead(\vspace{-.3cm}\\ \cmidrule(r{10pt}){2-3} \cmidrule(l{10pt}){4-5}  ) ///
postfoot(\vspace{-.3cm} \\ \hline \hline ) 


local stP "`ph' stats( N mdv, fmt( %11.0gc %8.2f)  labels("Individual-year obs."  "Mean dep. var."))" 

* without p values

esttab es_SE es_SEgE es_CreatOcc1_SE es_CreatOcc1_Emp 	using results/Tables/R_OLS_SECreatOcc.tex, ///
	replace  se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
	keep(Creativity CogAbility) refcat(Creativity ""  " " "", nolabel) ///
	order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
	cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) frag ///
	mtitles("(all individuals)" "(individual in work) \ \ \ " "\ \ \ (self-employed)" "(employees)") `text1' 	`stP'

esttab es_SEeduc es_SEgEeduc es_CreatOcc2_SE es_CreatOcc2_Emp 	using results/Tables/R_OLS_SECreatOcc.tex, ///
	append  se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
	keep(Creativity CogAbility) refcat(Creativity ""  " " "", nolabel) ///
	order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
	cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) frag ///
	mtitles("(all individuals)" "(individual in work) \ \ \ " " \ \ \ (self-employed)" "(employees)") `text2' 	`stP'
	
	
* with p values

esttab es_SE es_SEgE es_CreatOcc1_SE es_CreatOcc1_Emp 	using results/Tables/R_OLS_SECreatOccp.tex, ///
	replace  se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
	keep(Creativity CogAbility) refcat(Creativity ""  " " "", nolabel) ///
	order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
	cells(b(star fmt(3)) se(par) p(par([ ]) fmt(%9.5f)) ".") nogaps   alignment(c) frag ///
	mtitles("(all individuals)" "(individual in work)" "(self-employed)" "(employees)") `text1' 	`stP'

esttab es_SEeduc es_SEgEeduc es_CreatOcc2_SE es_CreatOcc2_Emp 	using results/Tables/R_OLS_SECreatOccp.tex, ///
	append  se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
	keep(Creativity CogAbility) refcat(Creativity ""  " " "", nolabel) ///
	order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
	cells(b(star fmt(3)) se(par) p(par([ ]) fmt(%9.5f)) ".") nogaps   alignment(c) frag ///
	mtitles("(all individuals)" "(individual in work)" "(self-employed)" "(employees)") `text2' 	`stP'
	
	
	
	
	
	
	
	
	
est drop _all
restore
